﻿#region Using Statements
using System;
using GameBase.Screens;
using Microsoft.Xna.Framework;
#endregion

namespace StarFox.ScreenComponents
{
    /// <summary>
    /// Implementation of the player starship.
    /// <para>Definition of player related members.</para>
    /// </summary>
    partial class StarShip
    {
        #region Fuel

        /// <summary>
        /// Gets or sets the StarShip's fuel level available.
        /// </summary>
        public float Fuel
        {
            get { return velCtrl.Fuel; }
            private set { velCtrl.Fuel = value; }
        }

        #endregion

        #region Health

        /// <summary>
        /// Gets or sets the StarShip's health level available.
        /// </summary>
        public float Health
        {
            get;
            private set;
        }

        /// <summary>
        /// Sets the duration of the StarShip's health when out of fuel.
        /// </summary>
        public TimeSpan HealthOutFuelDuration
        {
            private get;
            set;
        }

        #endregion

        #region Boost

        /// <summary>
        /// Gets or sets the StarShip's boost level available.
        /// </summary>
        public float Boost
        {
            get { return velCtrl.Boost; }
            private set { velCtrl.Boost = value; }
        }

        /// <summary>
        /// Raised on StarShip's attempt to accelerate or brake when out of boost.
        /// </summary>
        public event EventHandler<EventArgs> OutBoost;

        #endregion

        #region Bombs

        float bombModeTime;

        /// <summary>
        /// Gets or sets the StarShip's number of bombs available.
        /// </summary>
        public int Bombs
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets whether the StarShip's bomb mode is active.
        /// <para>In bomb mode, the StarShip is invulnerable to asteroid collisions.</para>
        /// </summary>
        public bool IsBombMode
        {
            get;
            private set;
        }

        /// <summary>
        /// Sets the duration of the StarShip's bomb mode.
        /// <para>In bomb mode, the StarShip is invulnerable to asteroid collisions.</para>
        /// </summary>
        public TimeSpan BombModeDuration
        {
            private get;
            set;
        }

        /// <summary>
        /// Raised on StarShip's bomb mode activation.
        /// </summary>
        public event EventHandler<EventArgs> BombModeActivated;

        #endregion

        #region Initialize_Player

        private void Initialize_Player()
        {
            Bombs = 0;
            Fuel = 1.0f;
            Health = 1.0f;
            Boost = 1.0f;
        }

        #endregion

        #region Update_Player

        private void Update_Player(GameTime gameTime)
        {
            if (Fuel == 0.0f)
            {
                float timeDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / HealthOutFuelDuration.TotalMilliseconds);
                Health -= timeDelta;
                Health = MathHelper.Clamp(Health, 0.0f, 1.0f);
            }

            if (Health == 0.0f)
            {
                IsBombMode = false; bombModeTime = 0.0f;
                return;
            }

            if (Boost == 0.0f && (input.IsBoost(Screen.PlayerIndex) || input.IsBrake(Screen.PlayerIndex))) { OutBoost(this, EventArgs.Empty); }

            if (!IsBombMode)
            {
                if (input.IsBomb(Screen.PlayerIndex) && Bombs > 0)
                {
                    Bombs--;
                    IsBombMode = true;
                    BombModeActivated(this, EventArgs.Empty);
                }
            }
            else
            {
                float timeDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / BombModeDuration.TotalMilliseconds);
                bombModeTime += timeDelta;
                bombModeTime = MathHelper.Clamp(bombModeTime, 0.0f, 1.0f);
                if (bombModeTime == 1.0f) { IsBombMode = false; bombModeTime = 0.0f; }
            }
        }

        #endregion
    }
}
